<?php

	include ("../includes/connect_db.php");
	include("../includes/core.php");

	extract($_POST, EXTR_SKIP);

	$json = array();
	switch ($act) {
		case 'monthviewagent':
			$sqlAgent = "SELECT * FROM agent";
			$queryAgent = mysql_query($sqlAgent);
			while ($barisAgent = mysql_fetch_array($queryAgent)) {
				$sql = "SELECT SUM((real_paid*(DATE(checkout)-DATE(checkin))) * $barisAgent[komisi] / 100) AS paid, 
								COUNT(id) AS tamu FROM front_office 
								WHERE `idagent` = '".$barisAgent["id"]."' 
								AND is_paid = 1 
								AND CONCAT(YEAR(checkout),'-',MONTH(checkout)) = '".$date."' 
								ORDER BY checkout DESC";
				$query = mysql_query($sql);
				if (mysql_num_rows($query) > 0) {
					while ($baris = mysql_fetch_array($query)) {
						$data = array();
						$data["name"] = $barisAgent["nama"];
						$data["paid"] = "IDR ".number_format($baris["paid"]);
						$data["tamu"] = $baris["tamu"];
						array_push($json, $data);
					}
				} else {
						$data = array();
						$data["name"] = $barisAgent["nama"];
						$data["paid"] = "IDR 0";
						$data["tamu"] = 0;
						array_push($json, $data);
				}
			}
			break;

		case 'yearviewagent':
			$sqlAgent = "SELECT * FROM agent";
			$queryAgent = mysql_query($sqlAgent);
			while ($barisAgent = mysql_fetch_array($queryAgent)) {
				$sql = "SELECT SUM((real_paid*(DATE(checkout)-DATE(checkin))) * $barisAgent[komisi] / 100) AS paid, 
								COUNT(id) AS tamu FROM front_office 
								WHERE `idagent` = '".$barisAgent["id"]."' 
								AND is_paid = 1 
								AND YEAR(checkout) = '".$date."'
								ORDER BY checkout DESC";
				$query = mysql_query($sql);
				if (mysql_num_rows($query) > 0) {
					while ($baris = mysql_fetch_array($query)) {
						$data = array();
						$data["name"] = $barisAgent["nama"];
						$data["paid"] = "IDR ".number_format($baris["paid"]);
						$data["tamu"] = $baris["tamu"];
						array_push($json, $data);
					}
				} else {
						$data = array();
						$data["name"] = $barisAgent["nama"];
						$data["paid"] = "IDR 0";
						$data["tamu"] = 0;
						array_push($json, $data);
				}
			}
			break;
	}

	echo json_encode($json);
?>